define(['config/appregister',"business/engDepartment/services/httpService","business/home/services/filterService"],function(app,httpService){
    app.directive("bigBanner",function($interval,$q){
        return {
            templateUrl:"app/business/engDepartment/template/bigBanner.html",
            link:function(scope,element,attrs){
                scope.dirFlag=false;
                //获取工程汇轮播图信息
                function getBannerList(){
                    var deferred = $q.defer();
                    var req={
                        type:'GET',
                        uri:'getBannerList',
                        removeCookie:true
                    };
                    httpService.bannerHttpService(req).success(function(data){
                        scope.bannerList=data.data;
                        console.log(scope.bannerList);
                        if(scope.bannerList.length==0){
                            scope.bannerList=[{logo:'them/imag/bigbanner-new.jpg'}];
                            scope.bannerFlag=false;
                            return;
                        }
                        if(scope.bannerList.length==1){
                            scope.bannerFlag=false;
                            return;
                        }
                        scope.bannerFlag=true;
                        scope.$apply();
                        deferred.resolve(scope.bannerList);
                    }).error(function(){
                        console.log('工程汇轮播图请求失败');
                        deferred.reject();
                    });
                    return deferred.promise;
                }

                //获取招聘信息
                function getRecruitList(){
                    var deferred = $q.defer();
                    var req={
                        type:'GET',
                        uri:'getNewJob',
                        data:{
                            count:10
                        },
                        removeCookie:true
                    };
                    httpService.companyHttpService(req).success(function(data){
                        scope.recruitList=data.data;
                        deferred.resolve(scope.recruitList);
                    }).error(function(){
                        console.log('招聘信息请求失败');
                        deferred.reject();
                    });
                    return deferred.promise;
                }

                /*大banner轮播开始*/
                getBannerList().then(function(data){
                    if(scope.bannerFlag){
                        var bannerBoxW=$(window).width();
                        var len=data.length+1;
                        $('.imgbox').css('width',bannerBoxW*len);
                        $('.imgbox>li').css('width',bannerBoxW);
                        $('.dot').css('marginLeft',-(22*(len-1)+19)/2);
                        $('.dot>li').eq(0).addClass('active');

                        //轮播
                        function Play(){
                            var i= 0,j=0;
                            this.move=function(dir,len,width,m){
                                if(m!=undefined){
                                    i=m;
                                    j=m;
                                }
                                if(dir!=''){
                                    if(dir=='next'){i++;j++}
                                    if(dir=='pre'){i--;j--}
                                }
                                if(j>len-1){j=0;}
                                if(i>len){i=1;$('.imgbox').css('left',0)}
                                if(i<0){i=len-1;j=len-1;$('.imgbox').css('left',-width*len)}
                                $('.dot>li').eq(j).addClass('active').siblings().removeClass('active');
                                $('.imgbox').stop().animate({left:-width*i},600);
                            }
                        }

                        var bannerPaly=new Play();
                        //左右箭头点击事件
                        scope.arrowCtrl=function(dir){
                            bannerPaly.move(dir,len-1,bannerBoxW);
                        };
                        //小banner自动播放
                        var timer=$interval(function(){
                            bannerPaly.move('next',len-1,bannerBoxW);
                        },2500);

                        //切换页面清除潜在定时器
                        scope.$on("$destroy", function() {
                            $interval.cancel( timer );
                            $interval.cancel( recruitTimer );
                        });

                        $('#banner').hover(function(){
                            scope.dirFlag=true;
                            $interval.cancel( timer );
                        },function(){
                            scope.dirFlag=false;
                            timer=$interval(function(){
                                bannerPaly.move('next',len-1,bannerBoxW);
                            },2500);
                        });
                        //小圆点hover
                        scope.hoverDot=function(m){
                            bannerPaly.move('',len-1,bannerBoxW,m);
                        }
                    }
                });
                /*大banner轮播结束*/

                /*招聘信息自动播放开始*/
                getRecruitList().then(function(data){
                    scope.recruitInfoLen=data.length;
                    if(scope.recruitInfoLen<=4){
                        $('.recruit-content>ul').css('height',56*4);
                        scope.flag=false;
                    }else{
                        $('.recruit-content>ul').css('height',56*scope.recruitInfoLen*2);
                        scope.flag=true;
                    }

                    if(scope.flag){
                        var recruitTimer=$interval(function(){
                            recruitInfoPlay();
                        },100);
                        var recruitInfoSpace=0;
                        function recruitInfoPlay(){
                            recruitInfoSpace+=1;
                            if(recruitInfoSpace>=56*scope.recruitInfoLen){
                                recruitInfoSpace=0;
                            }
                            $('.recruit-content>ul').css('marginTop',-recruitInfoSpace);
                        }
                        scope.$on("$destroy", function() {
                            $interval.cancel( recruitTimer );
                            $interval.cancel( timer );
                        });

                        $('.recruit-content').hover(function(){
                            $interval.cancel( recruitTimer );
                        },function(){
                            recruitTimer=$interval(function(){
                                recruitInfoPlay();
                            },100);
                        });
                    }
                });


                /*招聘信息自动播放结束*/
            }
        }
    });
});